package com.bytz.modules.cms.domain.impl.order;

import com.bytz.common.exception.BytzBootException;
import com.bytz.modules.cms.domain.MallOrderProcessor;
import com.bytz.modules.cms.domain.model.order.MallOrderProcessContext;
import com.bytz.modules.cms.order.constant.OrderConstants;
import com.bytz.modules.cms.order.entity.Order;
import com.bytz.modules.cms.order.model.OrderModel;
import com.bytz.modules.cms.order.service.IOrderService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Objects;

/**
 * 订单状态处理器 1
 * @author shanxuezhong
 */

public class MallOrderStatusProcessor extends MallOrderAbstractProcessor implements MallOrderProcessor {


    private IOrderService orderService;
    @Autowired
    public void setOrderService(IOrderService orderService) {
        this.orderService = orderService;
    }

    public MallOrderStatusProcessor(int sequence) {
        super(sequence);
    }

    @Override
    public void doProcess(MallOrderProcessContext context) {
        OrderModel order = context.getOrder();

        if (StringUtils.isNotBlank(order.getId())) {
            Order dbOrder = orderService.getById(order.getId());
            if (!OrderConstants.OrderStatus.CAO_GAO.equals(dbOrder.getStatus())) {
                throw new BytzBootException("订单已提交，无法编辑");
            }

            if (!Objects.equals(order.getOrderType(), dbOrder.getOrderType())) {
                throw new BytzBootException("订单类型不能更改");
            }

            if (!Objects.equals(context.getResellerId(), dbOrder.getResellerId())) {
                throw new BytzBootException("非此订单的所有者无法修改订单");
            }
        } else {
            order.setStatus(OrderConstants.OrderStatus.CAO_GAO);
        }
    }
}